/****************************************************
 * Description: DAOimpl for 用户
 * Copyright:   Copyright (c) 2013
 * Company:     beiwaionline
 * @author      bfsu
 * @version     1.0
 * @see
	HISTORY
    *  2013-08-08 bfsu Create File
**************************************************/

package com.bfsuolframework.security.dao.hibernate;

import java.util.List;

import com.bfsuolframework.security.entity.BaseUser;
import org.springframework.stereotype.Repository;

import com.bfsuolframework.security.dao.BaseUserDAO;
import com.bfsuolframework.core.controller.support.Pagination;
import com.bfsuolframework.core.dao.HibernateDAOSupport;
import com.bfsuolframework.core.dao.support.HqlParameter;

@Repository
public class BaseUserDAOImpl extends HibernateDAOSupport<BaseUser> implements BaseUserDAO {

    //通过 角色主键 查询 用户 集合
    private final String FIND_BASEUSER_BYROLEID = "FROM BaseUser user WHERE user.roles.id = :roleId";
	
    //通过 用户组主键 查询 用户 集合
    private final String FIND_BASEUSER_BYGROUPID = "select distinct  user from BaseUser user,Group g where  user in elements(g.users) and g.id = :groupId";
//    private final String FIND_BASEUSER_BYGROUPID = "FROM BaseUser user WHERE user.groups.id = :groupId";
	
    
    /**
     * 通过 角色主键 查询 用户
     * @return 用户 集合
     */
    public List<BaseUser> findUsersByRoleId(Long roleId){
        return findAll(FIND_BASEUSER_BYROLEID, HqlParameter.newInstance("roleId", roleId));
    }
	
    /**
     * 通过 用户组主键 查询 用户
     * @return 用户 集合
     */
    public List<BaseUser> findUsersByGroupId(Long groupId){
        return findAll(FIND_BASEUSER_BYGROUPID, HqlParameter.newInstance("groupId", groupId));
    }
	
    
	/* (non-Javadoc)
	 * @see BaseUserDAO#checkEmail(java.lang.String, java.lang.Long)
	 */
	public Boolean checkEmail(String email, Long id) {
		String hql = "select count(*) from BaseUser bu where bu.email =:email ";
		HqlParameter hqlParameter = HqlParameter.newInstance();

		if (id != null) {// 更新用户信息时,验证是否重复
			hql += " and bu.id !=:id ";
			hqlParameter.addParameter("id", id);
		}

		int count = getCount(hql, hqlParameter.addParameter("email", email));

		if (count > 0) {// 有多条
			return true;
		} else {
			return false;
		}

	}

	public Boolean checkLoginName(String loginName, Long id) {
		String hql = "select count(*) from BaseUser bu where bu.loginName =:loginName ";
		HqlParameter hqlParameter = HqlParameter.newInstance();

		if (id != null) {// 更新用户信息时,验证是否重复
			hql += " and bu.id !=:id ";
			hqlParameter.addParameter("id", id);
		}

		int count = getCount(hql, hqlParameter.addParameter("loginName", loginName));

		if (count > 0) {// 有多条
			return true;
		} else {
			return false;
		}
	}

	/* (non-Javadoc)
	 * @see BaseUserDAO#findUserByGroup(java.lang.String, java.lang.String, java.lang.String, java.lang.Long, com.bfsuolframework.core.controller.support.Pagination)
	 */
	@Override
	public List<BaseUser> findUserByGroup(String name, String loginName,
			String email,Long groupId, Pagination page,boolean isBelong) {
		String hql = "select distinct  user from BaseUser user,Group g where 1=1" ;;
		if(isBelong){
			hql = "select distinct  user from BaseUser user,Group g where 1=1" ;
					if(name!=null&&name.trim().length()>0){
						hql += " and user.userName like :userName";
					}
					
					if(loginName.trim().length()>0){
						hql += " and user.loginName = :loginName" ;
					}
					if(email!=null&&email.trim().length()>0){
						hql += " and user.email like :email";
					}
					
					hql +=" and user in elements(g.users) and g.id = :groupId";
		}else{
					if(name!=null&&name.trim().length()>0){
						hql += " and user.userName like :userName";
					}
					
					if(loginName.trim().length()>0){
						hql += " and user.loginName = :loginName" ;
					}
					if(email!=null&&email.trim().length()>0){
						hql += " and user.email like :email";
					}
			
					hql +=" and user not in elements(g.users) and g.id = :groupId";
		}
		HqlParameter hq = HqlParameter.newInstance("groupId", groupId);
		if(name!=null&&name.trim().length()>0){
			hq.addParameter("userName", "%"+name+"%");
		}
		
		if(loginName.trim().length()>0){
			hq.addParameter("loginName", loginName);;
		}
		if(email!=null&&email.trim().length()>0){
			hq.addParameter("email", "%"+email+"%");
		}
		return findAll(hql, hq, page.getStartRecord()-1,page.getPageSize());
	}

	/* (non-Javadoc)
	 * @see BaseUserDAO#getUserCountByGroup(java.lang.String, java.lang.String, java.lang.String, java.lang.Long, com.bfsuolframework.core.controller.support.Pagination)
	 */
	@Override
	public int getUserCountByGroup(String name, String loginName, String email,
			Long groupId, Pagination page,boolean isBelong) {
		String hql = "select COUNT(distinct user.id) from BaseUser user,Group g where 1=1" ;;
		if(isBelong){
					if(name!=null&&name.trim().length()>0){
						hql += " and user.userName like :userName";
					}
					
					if(loginName.trim().length()>0){
						hql += " and user.loginName = :loginName" ;
					}
					if(email!=null&&email.trim().length()>0){
						hql += " and user.email like :email";
					}
					
					hql +=" and user in elements(g.users) and g.id = :groupId";
		}else{
					if(name!=null&&name.trim().length()>0){
						hql += " and user.userName like :userName";
					}
					
					if(loginName.trim().length()>0){
						hql += " and user.loginName = :loginName" ;
					}
					if(email!=null&&email.trim().length()>0){
						hql += " and user.email like :email";
					}
			
					hql +=" and user not in elements(g.users) and g.id = :groupId";
		}
		HqlParameter hq = HqlParameter.newInstance("groupId", groupId);
		if(name!=null&&name.trim().length()>0){
			hq.addParameter("userName", "%"+name+"%");
		}
		
		if(loginName.trim().length()>0){
			hq.addParameter("loginName", loginName);;
		}
		if(email!=null&&email.trim().length()>0){
			hq.addParameter("email", "%"+email+"%");
		}
		return getCount(hql, hq);
	}

	@Override
	public BaseUser getUserByLoginName(String loginName) {
		String hql = "select distinct  user from BaseUser user where user.loginName=:loginName" ;
		HqlParameter hq = HqlParameter.newInstance();
		if(loginName.trim().length()>0){
			hq.addParameter("loginName", loginName);
		}
		return (BaseUser) this.getByHql(hql, hq);
	}

}